package org.springblade.modules.jimu.controller;

import cn.hutool.core.date.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.restaurant.constant.RestaurantConstant;
import org.springblade.modules.restaurant.entity.PerformanceRating;
import org.springblade.modules.restaurant.service.IBaseConfigService;
import org.springblade.modules.restaurant.service.IDashBoardService;
import org.springblade.modules.restaurant.service.IPerformanceRatingService;
import org.springblade.modules.restaurant.vo.SearchTimeVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author harry
 * @公众号 Harry技术
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/jimu/bi")
@Tag(name = "BI")
public class BIController {

	private final IBaseConfigService baseConfigService;
	private final IDashBoardService dashBoardService;
	private IPerformanceRatingService performanceRatingService;

	@GetMapping("/banner")
	@Operation(summary = "banner", description = "小程序首页轮播图")
	public R<List<String>> banner() {
		String banner = baseConfigService.getValueByKey(RestaurantConstant.MINI_BANNER);
		List<String> bannerList = Arrays.asList(banner.split(","));
		return R.data(bannerList);
	}

	@GetMapping("/todoList")
	@Operation(summary = "待办事项", description = "待办事项")
	public R todoList() {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);
		List<Map<String, Object>> list = dashBoardService.todoListByMini(userId, tenantId, role);
		return R.data(list);
	}

	@GetMapping("/taskStatistics")
	@Operation(summary = "任务统计", description = "任务统计")
	public R taskStatistics() {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);

		Map<String, Object> map = dashBoardService.taskStatisticsByMini(userId, tenantId, role);
		return R.data(map);
	}

	@GetMapping("/scoreStatistics")
	@Operation(summary = "评分统计", description = "评分统计")
	public R scoreStatistics() {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);

		Map<String, Object> map = dashBoardService.scoreStatisticsByMini(userId, tenantId, role);
		return R.data(map);
	}

	@GetMapping("/monthlyRate")
	@Operation(summary = "当月评分", description = "当月评分")
	public R monthlyRate() {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String month = DateUtil.format(DateUtil.date(), "yyyy-MM");
		PerformanceRating performanceRating = performanceRatingService.lambdaQuery()
			.eq(PerformanceRating::getUserId, userId)
			.eq(PerformanceRating::getMonth, month).one();
		BigDecimal score = performanceRating == null ? BigDecimal.ZERO : performanceRating.getScore();
		return R.data(score);
	}


	@GetMapping("/dayIncomeExpenditureStatistics")
	@Operation(summary = "日收入支出统计", description = "日收入支出统计")
	public R dayIncomeExpenditureStatistics(SearchTimeVO searchTime) {

		String startTime = searchTime.getStartTime();
		String endTime = searchTime.getEndTime();
		if (startTime == null || endTime == null) {
			startTime = DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -7));
			endTime = DateUtil.formatDate(DateUtil.date());
			searchTime.setStartTime(startTime);
			searchTime.setEndTime(endTime);
		}

		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);
		Map<String, Object> map = dashBoardService.dayIncomeExpenditureStatisticsByMini(userId, tenantId, role,searchTime);
		return R.data(map);
	}

	@GetMapping("/monthIncomeExpenditureStatistics")
	@Operation(summary = "月收入支出统计", description = "月收入支出统计")
	public R monthIncomeExpenditureStatistics() {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);
		Map<String, Object> map = dashBoardService.monthIncomeExpenditureStatisticsByMini(userId, tenantId, role);
		return R.data(map);
	}

	// 点到统计
	@GetMapping("/attendanceStatistics")
	@Operation(summary = "点到统计", description = "点到统计")
	public R attendanceStatistics(SearchTimeVO searchTime) {

		String startTime = searchTime.getStartTime();
		String endTime = searchTime.getEndTime();
		if (startTime == null || endTime == null) {
			startTime = DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -7));
			endTime = DateUtil.formatDate(DateUtil.date());
			searchTime.setStartTime(startTime);
			searchTime.setEndTime(endTime);
		}
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);

		Map<String, Object> map = dashBoardService.attendanceStatisticsByMini(userId, tenantId, role, searchTime);
		return R.data(map);
	}

	@GetMapping("/todayIncomeExpenditureStatistics")
	@Operation(summary = "昨日收入支出统计", description = "昨日收入支出统计")
	public R todayIncomeExpenditureStatistics() {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);
		String day = DateUtil.formatDate(DateUtil.yesterday());

		Map<String, Object> map = dashBoardService.todayIncomeExpenditureStatistics(userId, tenantId, role, day);
		return R.data(map);
	}

	// 各部门考勤人数统计
	@GetMapping("/deptUserStatistics")
	@Operation(summary = "各部门考勤人数统计", description = "各部门考勤人数统计")
	public R deptUserStatistics() {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		// 获取当前登录用户ID
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);

		return R.data(dashBoardService.deptUserStatistics(userId, tenantId, role));
	}


}
